package files;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.compress.utils.Lists;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Optional;

/**
 * @ClassName: JsonToExcelNew
 * @Description: 暂时废弃
 * @Author: zyy
 * @Date: 2020/08/14 19:24
 * @Version: 1.0
 */
public class JsonToExcelNew {

    public static final String RESPONSE_CODE_SUCCESS = "999999";

    public static void main(String[] args) {
        //读取文件
        String jsonString = null;

        try (InputStream inputStream = new FileInputStream("D:\\document\\json.txt");
             InputStreamReader streamReader = new InputStreamReader(inputStream);
             BufferedReader bufferedReader = new BufferedReader(streamReader)) {
            StringBuffer stringBuffer = new StringBuffer();
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                stringBuffer.append(line);
            }
            jsonString = stringBuffer.toString();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //JSON String to Map
        Map<String, Object> paramMap = JSONObject.parseObject(jsonString);
        //准备数据
        String responseCode = Optional.ofNullable(paramMap)
                .map(map -> (String) map.get("responseCode"))
                .orElse(null);
        if (!RESPONSE_CODE_SUCCESS.equals(responseCode)) {
            return;
        }
        Map<String, Object> applyMap = Optional.ofNullable(paramMap)
                .map(map -> (Map<String, Object>) map.get("result"))
                .map(resultMap -> (Map<String, Object>) resultMap.get("APPLY"))
                .orElse(null);
        if (MapUtils.isEmpty(applyMap)) {
            return;
        }
        System.out.println("投保报文：" + applyMap);
        List<Map<String, Object>> paramInList = (List<Map<String, Object>>) applyMap.get("paramIn");
        List<Map<String, Object>> paramOutList = (List<Map<String, Object>>) applyMap.get("paramOut");
        List<Map<String, Object>> responseCodeList = (List<Map<String, Object>>) applyMap.get("responseCode");
        System.out.println("----入参报文：" + paramInList);
        System.out.println("----出参参报文：" + paramOutList);
        System.out.println("----响应码报文：" + responseCodeList);
        List<List<String>> list = Lists.newArrayList();
        //转二维数组
        //--入参报文转二维数组
        //多少行多少列
        //字段 是否必填 字段类型 字段描述


        //总的行数（统计nodeName的个数）
//        int totalRow = 0;
        //总的列数（统计深度+3）
//        int totalCell = 0;
        totalRow = totalRow + paramOutList.size();
        ;
        ListIterator<Map<String, Object>> paramOutIterator = paramOutList.listIterator();
        while (paramOutIterator.hasNext()) {
            Map<String, Object> paramOutMap = paramOutIterator.next();
            recursive(paramMap);
        }


        System.out.println("totalRow：" + totalRow + " totalCell：" + (depth++));

        for (int i = 0; i < paramOutList.size(); i++) {

        }

        //--出参报文转二维数组
        //--响应码转二维数组

        try (//创建工作簿
             Workbook workbook = new SXSSFWorkbook(1000);
             //输出流
             OutputStream outputStream = new FileOutputStream("D:\\document\\测试文档.xlsx")) {
            //创建工作表
            Sheet policySheet = workbook.createSheet("投保");
            for (int i = 0; i < 10; i++) {
                //行
                Row sheetRow = policySheet.createRow(i);
                for (int j = 0; j < 10; j++) {
                    //列
                    Cell rowCell = sheetRow.createCell(j);
                    // i 行 j 列的值
                    rowCell.setCellValue(i + "" + j);
                }
            }
            //写
            workbook.write(outputStream);
            System.out.println("生成完成");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private static int depth = 0;
    private static int totalRow = 0;

    public static void recursive(Map<String, Object> map) {
        List<Map<String, Object>> subNodeList = (List<Map<String, Object>>) map.get("subNodeList");
        if (subNodeList == null || subNodeList.size() == 0) {
            return;
        }
        depth++;
        for (Map<String, Object> subNodeMap : subNodeList) {
            recursive(subNodeMap);
        }
        totalRow = totalRow + subNodeList.size();


    }
}
